from mitmproxy import ctx
import json
from pymongo import MongoClient
from datetime import datetime


class DB(object):
    def __init__(self):
        self.client = MongoClient('127.0.0.1', 27017)
        self.db = self.client["DouYin"]
        self.collection = self.db["pinglun"]
        self.collection1 = self.db["小红书_笔记_澳门网红餐厅"]
    def insertItem(self, data):
        self.collection.insert_one(data)
    def insertItem_1(self, data):
        self.collection1.insert_one(data)
    def find(self, query):
        return self.collection1.find_one(query)

db = DB()

# mitmdump.exe -p 8888 -s E:\Workspaces\Python\Spider\小红书\xiaohongshu_pinglun_script.py
# 所有发出的请求数据包都会被这个方法所处理
# 所谓的处理，我们这里只是打印一下一些项；当然可以修改这些项的值直接给这些项赋值即可
def request(flow):
    # # 获取请求对象
    # request = flow.request
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印请求的url
    # info(request.url)
    # # 打印请求方法
    # info(request.method)
    # # 打印host头
    # info(request.host)
    # # 打印请求端口
    # info(str(request.port))
    # # 打印所有请求头部
    # info(str(request.headers))
    # # 打印cookie头
    # info(str(request.cookies))
    pass

# 所有服务器响应的数据包都会被这个方法处理
# 所谓的处理，我们这里只是打印一下一些项
def response(flow):
    # 获取响应对象
    response = flow.response
    # # 实例化输出类
    # info = ctx.log.info
    # # 打印响应码
    # info(str(response.status_code))
    # # 打印所有头部
    # info(str(response.headers))
    # # 打印cookie头部
    # info(str(response.cookies))
    # # 打印响应报文内容
    # info(str(response.text))

    # ctx.log.info(response.text)
    try:
        # 文章无视频-笔记
        if 'api/sns/v1/note/feed' in flow.request.url:
            ctx.log.info("============笔记数据=============")
            datajson = json.loads(response.text)
            dict_data = dict()
            if datajson.get('data') is not None:
                if len(datajson.get('data')) > 0:
                    dict_data['类型'] = 'Note'
                    dict_data['ID'] = datajson['data'][0]['note_list'][0]['id']  # 笔记ID
                    dict_data['标题'] = datajson['data'][0]['note_list'][0]['title']  # 笔记标题
                    dict_data['内容'] = datajson['data'][0]['note_list'][0]['desc']  # 笔记内容
                    dict_data['点赞数量'] = datajson['data'][0]['note_list'][0]['liked_count']  # 点赞数量
                    dict_data['收藏数量'] = datajson['data'][0]['note_list'][0]['collected_count']  # 收藏数量
                    dict_data['评论数量'] = datajson['data'][0]['note_list'][0]['comments_count']  # 评论数量
                    dateArray = datetime.fromtimestamp(int(datajson['data'][0]['note_list'][0]['time']))  # 发布时间
                    dict_data['发布时间'] = dateArray.strftime('%Y-%m-%d %H:%M:%S')
                    dict_data['用户昵称'] = datajson['data'][0]['user']['nickname']  # 用户昵称
                    dict_data['用户ID'] = datajson['data'][0]['user']['id']  # 用户ID
                    db.insertItem_1(dict_data)
                    # ctx.log.info(str(dict_data))

                    ctx.log.info("=======ok========")
        # # 视频-笔记
        # if 'xiaohongshu.com/api/sns/v2/note' in flow.request.url:
        #     ctx.log.info("============【视频】用户数据=============")
        #     datajson = json.loads(response.text)
        #     dict_data = dict()
        #
        #     if datajson.get('data') is not None:
        #         if len(datajson.get('data')) > 0:
        #             dict_data['type'] = 'Video'
        #             dict_data['note_id'] = datajson['data'][0]['id']  # 视频笔记ID
        #             dict_data['title'] = datajson['data'][0]['title']  # 视频标题
        #             dict_data['desc'] = datajson['data'][0]['desc']  # 视频内容
        #             dict_data['liked_count1'] = datajson['data'][0]['liked_count']  # 点赞数量
        #             dict_data['collected_count1'] = datajson['data'][0]['collected_count']  # 收藏数量
        #             dict_data['comments_count1'] = datajson['data'][0]['comments_count']  # 评论数量
        #             dateArray = datetime.fromtimestamp(int(datajson['data'][0]['time'])) # 发布时间
        #             dict_data['commentTime1'] = dateArray.strftime('%Y-%m-%d %H:%M:%S')
        #             dict_data['nickname1'] = datajson['data'][0]['user']['nickname'] # 用户昵称
        #             dict_data['uid1'] = datajson['data'][0]['user']['id'] # 用户ID
        #             db.insertItem_1(dict_data)
        #             # ctx.log.info(str(dict_data))
        #
        #             ctx.log.info("=======ok========")
        # # 评论内容
        # if 'xiaohongshu.com/api/sns/v5/note' in flow.request.url:
        #     ctx.log.info("============获取评论数据=============")
        #     datajson = json.loads(response.text)
        #     if datajson['data']['comments'] is not None:
        #         # 查询数据库提取用户的信息
        #         myquery = {'uid1': datajson['data']['user_id'], 'note_id': datajson['data']['comments'][0]['note_id']}
        #         mydoc = db.find(myquery)
        #         if mydoc is not None:
        #             title = mydoc.get('title')
        #             desc = mydoc.get('desc')
        #             liked_count1 = mydoc.get('liked_count1')
        #             comments_count1 = mydoc.get('comments_count1')
        #             commentTime1 = mydoc.get('commentTime1')
        #             type = mydoc.get('type')
        #         else:
        #             title = ""
        #             desc = ""
        #             liked_count1 = ""
        #             comments_count1 = ""
        #             commentTime1 = ""
        #             type = ""
        #         print("---------提取数据------------")
        #         for d in datajson['data']['comments']:
        #             dict_data = dict()
        #             dict_data['uid_biji'] = datajson['data']['user_id'] # 发帖人的ID
        #             dict_data['content'] = d['content']  # 当前评论内容
        #             dict_data['note_id'] = d['note_id']  # 笔记ID
        #             dateArray = datetime.fromtimestamp(int(d['time']))
        #             dict_data['commentTime'] = dateArray.strftime('%Y-%m-%d %H:%M:%S')
        #             dict_data['nickname'] = d['user']['nickname']
        #             dict_data['uid'] = d['user']['userid']
        #
        #             dict_data['liked_count1'] = liked_count1
        #             dict_data['comments_count1'] = comments_count1
        #             dict_data['commentTime1'] = commentTime1
        #             dict_data['title'] = title
        #             dict_data['desc'] = desc
        #             dict_data['type'] = type
        #
        #             # ctx.log.info(str(datajson))
        #             db.insertItem(dict_data)
        #             ctx.log.info("=======ok========")

        # # 搜索内容
        # if '/api/sns/v10/search/notes' in flow.request.url:
        #     ctx.log.info("============笔记内容=============")
        #     datajson = json.loads(response.text)
        #     for d in datajson['data']['items']:
        #         if d.get('note') is not None:
        #             dict_data = dict()
        #             dict_data['type'] = "深圳旅游攻略"
        #             dict_data['note_id'] = d['note']['id']  # 笔记ID
        #             dict_data['title'] = d['note']['title']  # 笔记标题
        #             dict_data['desc'] = d['note']['desc']  # 笔记内容
        #             dict_data['liked_count'] = d['note']['liked_count']  # 点赞数量
        #             dict_data['nickname'] = d['note']['user']['nickname']  # 用户昵称
        #             dict_data['userid'] = d['note']['user']['userid']  # 用户ID
        #
        #             db.insertItem_1(dict_data)
        #             # ctx.log.info(str(dict_data))
        #
        #             ctx.log.alert("=======ok========")

    except Exception as ex:
        ctx.log.warn("============错误=============")
        ctx.log.error(ex)





